const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    textureItem: 0
  },
  
  onLoad(options){
    this.setData({
      detailId: options.detail_id
    }, callback => {
      this.getGoodDetail();
    });
  },

  onShareAppMessage(){

  },

  collect(e) {
    let id = e.currentTarget.dataset.id;
    this.collectGood(id);
  },

  getSpec(e) {
    let td = this.data,
      ed = e.currentTarget.dataset;

    td.spec.spec_attr[ed.pindex].activeItem = ed.cindex;
    this.setData({
      spec: td.spec
    });
  },

  config(e){
    let td = this.data,
        spec = td.spec,
        selected_spec = [];

    spec.spec_attr.forEach(item => {
      if (item.group_name == '尺寸'){
        selected_spec.push(item.spec_items[item.activeItem]);
      }
    });
    let detail_config = {
      size: selected_spec[0].spec_value.split('*'),
      imgUrl: td.detail.image,
      detailId: td.detailId,
      specAttr: spec.spec_attr
    };

    wx.navigateTo({
      url: '/pages/subpage/custom-made/index/index?detail_config=' + JSON.stringify(detail_config)
    })
  },


  preview() {
    let td = this.data,
      preview_info = {
        imgUrl: td.detail.image,
        iframeimage: ''
      };

    wx.navigateTo({
      url: '/pages/subpage/custom-made/preview/preview?preview_info=' + JSON.stringify(preview_info)
    })
  },

  buy(){
    this.createOrder();
  },

  /**
   * 获取商品详情
   * @param id
   * */
  getGoodDetail() {
    let td = this.data;

    AJAX.goodDetail({
      loadType: 'bar',
      goods_id: td.detailId
    }).then(([status, res, msg]) => {
      if (status) {
        this.setData({
          detail: res.detail
        });

        if (res.specData && Object.keys(res.specData).length > 0) {
          let spec = res.specData;
          spec.spec_attr.forEach(item => {
            item.activeItem = 0;
          });

          this.setData({ spec });
        }
      } else {
        $wx.shosToast(msg);
      }
    });
  },

  /**
   * 收藏商品
   * @param goods_id
   * */
  collectGood(id) {
    let _this = this, td = _this.data;
    AJAX.collectGood({
      loadType: 'bar',
      goods_id: id
    }).then(([status, res, msg]) => {
      $wx.showToast(msg, {
        success() {
          td.detail.is_conllect = res.is_conllect;
          _this.setData({
            detail: td.detail
          });
        }
      });
    });
  },

  createOrder() {
    let td = this.data, chicun, caizhi;
    td.spec.spec_attr && td.spec.spec_attr.map(item => {
      if (item.group_name === '尺寸') {
        chicun = item.spec_items[item.activeItem].item_id;
      }
      if (item.group_name === '材质') {
        caizhi = item.spec_items[item.activeItem].item_id;
      }
    });
    AJAX.createOrder({
      loadType: 'default',
      loadMsg: '加载中...',
      type: 2,
      goods_id: td.detailId,
      goods_num: 1,
      chicun,
      caizhi
    }).then(([status, res, msg]) => {
      if (status) {
        wx.setStorageSync("orderDetail", res);
        wx.navigateTo({
          url: '/pages/subpage/order/detail/detail'
        })
      } else {
        $wx.showToast(msg);
      }
    });
  },
});